package org.example.mall.admin.controller.goods;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.example.mall.admin.constant.vaild.C;
import org.example.mall.admin.constant.vaild.Q;
import org.example.mall.admin.constant.vaild.U;
import org.example.mall.admin.model.entity.goods.SpecParams;
import org.example.mall.admin.model.vo.goods.GroupSpecParamsVo;
import org.example.mall.admin.service.goods.SpecParamsService;
import org.example.mall.admin.util.BusOp;
import org.example.mall.admin.util.R;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 规格模板值 前端控制器
 * </p>
 *
 * @author hzq
 * @since 2022-01-24
 */
@RestController
@RequestMapping("/goods/specParams")
@AllArgsConstructor
public class SpecParamsController {

    private final SpecParamsService specParamsService;

    @GetMapping("/{id}")
    @ApiOperation("根据id查询")
    public R<SpecParams> getById(@PathVariable Integer id) {
        return R.ofSuccess(specParamsService.getById(id));
    }

    @GetMapping
    @ApiOperation("查询分组全部属性列表")
    public R<List<SpecParams>> list(@Validated(Q.class) SpecParams params) {
        LambdaQueryWrapper<SpecParams> wrapper = Wrappers.<SpecParams>lambdaQuery().eq(SpecParams::getGroupId, params.getGroupId()).orderByAsc(SpecParams::getSort);
        return R.ofSuccess(specParamsService.list(wrapper));
    }

    @GetMapping("/page")
    @ApiOperation("分页查询分组属性列表")
    public R<IPage<SpecParams>> page(Page page, @Validated(Q.class) SpecParams params) {
        LambdaQueryWrapper<SpecParams> wrapper = Wrappers.<SpecParams>lambdaQuery().eq(SpecParams::getGroupId, params.getGroupId()).orderByAsc(SpecParams::getSort);
        return R.ofSuccess(specParamsService.page(page, wrapper));
    }


    @PostMapping
    @ApiOperation("添加规格模板分组属性")
    @PreAuthorize("hasAnyAuthority('sys_label_all','goods_group_spec_add')")
    public R<Boolean> add(@RequestBody @Validated(C.class) SpecParams params) {
        return R.ofSuccess("添加分组属性成功",
                BusOp.mustSucceed(specParamsService.save(params), "添加分组属性失败"));
    }

    @PutMapping
    @ApiOperation("修改规格模板分组属性")
    @PreAuthorize("hasAnyAuthority('sys_label_all','goods_group_spec_edit')")
    public R<Boolean> edit(@RequestBody @Validated(U.class) SpecParams params) {
        return R.ofSuccess("修改分组属性成功",
                BusOp.mustSucceed(specParamsService.updateById(params), "修改分组属性失败"));
    }

    @DeleteMapping("/{id}")
    @ApiOperation("删除规格模板分组属性")
    @PreAuthorize("hasAnyAuthority('sys_label_all','goods_group_spec_del')")
    public R<Boolean> del(@PathVariable Integer id) {
        return R.ofSuccess("删除分组属性成功",
                BusOp.mustSucceed(specParamsService.removeById(id), "删除分组属性失败"));
    }

    @GetMapping("/groupSpecList/{categoryId}")
    @ApiOperation("通过分类id查询规格模板参数")
    public R<List<GroupSpecParamsVo>> groupSpecList(@PathVariable Integer categoryId) {
        return R.ofSuccess(specParamsService.groupSpecList(categoryId));
    }
}

